﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data.SqlClient;


public partial class ybqy_t1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection sqlcon = new SqlConnection();
        sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
        sqlcon.Open();

        SqlCommand sqlcmd = new SqlCommand("select map_name2 from F_FACTMAP where ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"], sqlcon);

        SqlDataReader reader = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);



        if (!IsPostBack)
        {
            if (reader.Read())
            {
                if (reader["map_name2"].ToString() == null || reader["map_name2"].ToString() == "")
                {
                    return;
                }
                string fileName = reader["map_name2"].ToString();
                Image1.ImageUrl = "~/fileUpload/" + fileName;
                string path = Server.MapPath("~/fileUpload/");
                string sourcePath = path;
                if (!Directory.Exists(path))
                {  //判断是否存在uploadfile路径                     Directory.CreateDirectory(path);                 } 
                    //if (!Directory.Exists(sourcePath))    //判断 、创建原图片路径                 { 
                    //    Directory.CreateDirectory(sourcePath);
                }
                //上传原图片 

                Bitmap oldbitmap = new Bitmap(sourcePath + fileName);
                int Height = oldbitmap.Height;
                int Width = oldbitmap.Width;
                Image1.Height = 400;
                Image1.Width = 700;

                int nwidth = 400 * Width / Height;
                if (nwidth > 700)
                {
                    Image1.Height = 700 * Height / Width;
                }
                else
                {
                    Image1.Width = 400 * Width / Height;
                }

                oldbitmap.Dispose();
                sqlcon.Close();
            }
            else
            {
                String SqlCmd = "INSERT INTO F_FACTMAP (qiyeid, ShenBaoNianDu,fact_id,data_year) valueS (";
                SqlCmd = SqlCmd + Session["qiyeid"] + ", " + Session["ShenBaoNianDu"] + ",'1'," + Session["ShenBaoNianDu"] + ")";
                SqlCommand CmdObj = new SqlCommand(SqlCmd, sqlcon);
                reader.Close();
                sqlcon.Open();
                CmdObj.ExecuteNonQuery();
                sqlcon.Close();

            }

        }

    }
    private static Object ForCreateGUIDLock = new Object();
    public static string CreateGUID()
    {
        lock (ForCreateGUIDLock)
        {
            return Guid.NewGuid().ToString("N");
        }
    }

    protected HtmlInputFile UP_FILE; //HtmlControl、WebControls控件对象 
    protected TextBox txtDescription;
    protected Label txtMessage;
    protected Int32 FileLength = 0; //记录文件长度变量 

    protected void Button3_Click(object sender, EventArgs e)
    {
        try
        {
            Label3.Text = "";

            string fullname = FileUpload1.FileName.ToString();
            string filePath = FileUpload1.PostedFile.FileName;
            string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1);
            string fileName = "t2" + Session["qiyeid"] + Session["ShenBaoNianDu"] + "." + typ2;

            System.Web.HttpPostedFile m_file = FileUpload1.PostedFile;

            if (m_file == null)
            { return; }

            if (m_file.ContentLength > 10420000)
            { Label3.Text = "图片太大"; return; }

            if (typ2 == "gif" || typ2 == "jpg" || typ2 == "bmp" || typ2 == "png")
            {
                string path = Server.MapPath("~/fileUpload/");
                string sourcePath = path;
                if (!Directory.Exists(path))
                {  //判断是否存在uploadfile路径                     Directory.CreateDirectory(path);                 } 
                    if (!Directory.Exists(sourcePath))    //判断 、创建原图片路径                 { 
                        Directory.CreateDirectory(sourcePath);
                }
                //上传原图片 
                FileUpload1.SaveAs(sourcePath + fileName);

                FileUpload1.Dispose();
                Bitmap oldbitmap = new Bitmap(sourcePath + fileName);
                int Height = oldbitmap.Height;
                int Width = oldbitmap.Width;
                Image1.Height = 400;
                Image1.Width = 700;

                int nwidth = 400 * Width / Height;
                if (nwidth > 700)
                {
                    Image1.Height = 700 * Height / Width;
                }
                else
                {
                    Image1.Width = 400 * Width / Height;
                }
                oldbitmap.Dispose();
                Image1.ImageUrl = "~/fileUpload/" + fileName;
                Label3.Text = "上传文件成功！";
                //Response.Write("<script>alert('上传文件成功！')</script>");

                HttpPostedFile UpFile = FileUpload1.PostedFile; //HttpPostedFile对象，用于读取图象文件属性 
                FileLength = UpFile.ContentLength; //记录文件长度 

                try
                {
                    if (FileLength == 0)
                    { //文件长度为零时 
                        Label3.Text = "请你选择你要上传的文件";
                    }
                    else
                    {
                        Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组 
                        Stream StreamObject = UpFile.InputStream; //建立数据流对像 
                        //读取图象文件数据，FileByteArray为数据储存体，0为数据指针位置、FileLnegth为数据长度 
                        StreamObject.Read(FileByteArray, 0, FileLength);
                        //建立SQL Server链接 
                        SqlConnection sqlcon = new SqlConnection();
                        sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
                        sqlcon.Open();
                        String SqlCmd = "delete F_FACTMAP_temp ";
                        SqlCommand CmdObj = new SqlCommand(SqlCmd, sqlcon);
                        CmdObj.ExecuteNonQuery();
                        SqlCmd = "insert into F_FACTMAP_temp (aa)values (@Image)";
                        CmdObj.CommandText = SqlCmd;
                        CmdObj.Parameters.Add("@Image", SqlDbType.Image, FileLength).Value = FileByteArray;
                        CmdObj.ExecuteNonQuery();
                        SqlCmd = "update F_FACTMAP set fact_map2=null where ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"];
                        CmdObj.CommandText = SqlCmd;
                        //CmdObj.Parameters.Add("@Image", SqlDbType.Image, FileLength).Value = FileByteArray;
                        //CmdObj.ExecuteNonQuery();
                        SqlCmd = "update F_FACTMAP set fact_map2=b.aa,map_name2='" + fileName + "' from F_FACTMAP a,F_FACTMAP_temp b where a.ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and a.qiyeid=" + Session["qiyeid"];
                        CmdObj.CommandText = SqlCmd;
                        //CmdObj.Parameters.Add("@Image", SqlDbType.Image, FileLength).Value = FileByteArray;
                        CmdObj.ExecuteNonQuery();
                        sqlcon.Close();
                    }
                }
                catch (Exception ex)
                {
                    Label3.Text = ex.Message.ToString();
                    throw;
                }

            }
            else
            {
                Label3.Text = "文件类型不支持";
            }
        }
        catch (Exception ex)
        {
            //Response.Write("<script>alert('上传文件失败！')</script>");
            Label3.Text = ex.Message.ToString();
        }
    }
}